﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Business;
using Data;
using DataAccess.i2iPortalTableAdapters;

public partial class admin_Mng_Machine : @base {
    MachinesOnSitesTableAdapter machinesOnSiteTableAdapter = new MachinesOnSitesTableAdapter();
    private Data.i2iPortal.MachinesOnSitesDataTable dtMachines;
    private Encryption objenc = new Encryption();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["dtMachines"] != null)
        {
            dtMachines = (Data.i2iPortal.MachinesOnSitesDataTable)Session["dtMachines"];
        }
        if (!IsPostBack)
        {
            var ltItems = new List<ListItem>();
            var sta = new SitesTableAdapter();
            var allSites = sta.GetData();
            ltItems.Add(new ListItem()
                            {
                                Text = "All",
                                Value = "All",
                            });
            foreach (Data.i2iPortal.SitesRow  site in allSites)
            {
                ltItems.Add(new ListItem()
                                {
                                    Text = site.siteName,
                                    Value = site.site_id.ToString(),
                                });
            }
            dpSites.DataSource = ltItems;
            dpSites.DataBind();
            dtMachines = machinesOnSiteTableAdapter.GetData();
            GridViewMachines.DataSource = dtMachines;
            GridViewMachines.DataBind();
            Session["dtMachines"] = dtMachines;
        }
    }
    /// <summary>
    /// Handles the SelectedIndexChanged event of the GridViewMachines control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    /// <action>go to View_Machine.aspx with machine_id as query string</action>
    protected void GridViewMachines_SelectedIndexChanged(object sender, EventArgs e)
    {
        string machine_id = GridViewMachines.SelectedDataKey.Value.ToString();
        var row = GridViewMachines.SelectedRow;
        string machineName = row.Cells[0].Text;
        MACHINEID = ToInt(machine_id);
        MACHINENAME = machineName;
        Response.Redirect("View_Machine.aspx?machine_id=" + machine_id);
    }
    /// <summary>
    /// Handles the Click event of the CreateMachineButton control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    /// <action>go to Create_Machine.aspx</action>
    protected void CreateMachineButton_Click(object sender, EventArgs e) {
        var encodedUrl = objenc.EncodeUrl(dpSites.SelectedValue.ToString());
        Server.Transfer("Create_Machine.aspx?site_id=" + encodedUrl);
     }

    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {
        Session.Contents.Remove("UserDetails");
        Session.Abandon();
        Session.Contents.RemoveAll();
        FormsAuthentication.SignOut();
        Response.Redirect("../Default.aspx");
    }

    protected void GetMachinesBySiteID(object sender, EventArgs e)
    {
        RefreshDataSource();
    }

    private void RefreshDataSource()
    {
        var selectedSiteID = dpSites.SelectedValue;
        if (!selectedSiteID.Equals("All"))
        {
            var dtMachinesToAdd = dtMachines.Where(machine => machine.site_id == Int32.Parse(selectedSiteID));
            GridViewMachines.DataSource = dtMachinesToAdd.ToList();
        }
        else
        {
            GridViewMachines.DataSource = dtMachines;
        }
        GridViewMachines.DataBind();
    }

    protected void GridViewMachines_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewMachines.PageIndex = e.NewPageIndex;
        RefreshDataSource();
    }
}
